import { _decorator, Component, find, instantiate, JsonAsset, Label, Node, Prefab, resources, Sprite, SpriteFrame, tween, v3 } from 'cc';
import { UILayerMgr } from '../utils/UILayerMgr';
import { Dictionary } from '../utils/Dictionary';
import { UserInfo } from '../UserInfo';
import { randomNum } from '../utils/utils';
import { Adsdk } from './Adsdk';
const { ccclass, property } = _decorator;

@ccclass('Main')
export class Main extends Component {

    @property(Label)
    userNameLabel:Label;
    @property(Label)
    staminaNumLabel:Label;
    @property(Sprite)
    userAvatarSp:Sprite;
    @property(Prefab)
    minTipPre:Prefab;


    @property(JsonAsset)
    songCongfi: JsonAsset;

    static ins:Main=null;
    songCongfiDic: Dictionary<number, any> = new Dictionary();

    nameAllArr=["烟雨","爱吃布丁","大小姐驾到","c春暖花开","朝花夕拾aa","hahah","暴龙神zhan士"];
    avatarTotalNum=9;//头像总数

    protected onLoad(): void {
        UILayerMgr.getIns.initLayerPrePool();

        Main.ins=this;
    }

    initHomeUI() {
        Adsdk.instance.showInterstitialAd();
        this.userNameLabel.string = UserInfo.userName;
        this.staminaNumLabel.string = UserInfo.staminaNum+"";

        resources.load(`avatar/${UserInfo.userAvatar}/spriteFrame`, SpriteFrame, (err, res) => {
            this.userAvatarSp.spriteFrame = res;
        })

    }

    protected start(): void {

        this.songCongfi.json.forEach((ele) => {
            this.songCongfiDic.set(ele.songType, ele);
        })

        //缓存

        if (localStorage.getItem("userInfo")) {
            let _userInfo = JSON.parse(localStorage.getItem("userInfo"));
            UserInfo.userName = _userInfo.userName;
            UserInfo.userAvatar = _userInfo.userAvatar;
            UserInfo.staminaNum = _userInfo.staminaNum;
            UserInfo.taskIsTodayReceiveReward = _userInfo.taskIsTodayReceiveReward;
            UserInfo.todayWinPkGameNum = _userInfo.todayWinPkGameNum;
            UserInfo.taskIsTodayReceiveWinOnePkReward = _userInfo.taskIsTodayReceiveWinOnePkReward;
            UserInfo.taskIsTodayReceiveGateWinOneReward=_userInfo.taskIsTodayReceiveGateWinOneReward;
            UserInfo.taskTodayWinGateNum = _userInfo.taskTodayWinGateNum;
            UserInfo.gateLevel = _userInfo.gateLevel;

            const now = new Date();
            const nowDate = new Date(now.getFullYear(), now.getMonth(), now.getDate());
            if (localStorage.getItem('loginTodayTime') != nowDate.toLocaleString()) {//如果不是今天,重置今天数据
                localStorage.setItem('loginTodayTime', nowDate.toLocaleString());

                UserInfo.taskIsTodayReceiveReward = false;
                UserInfo.todayWinPkGameNum = 0;
                UserInfo.taskIsTodayReceiveWinOnePkReward=false;
                UserInfo.taskTodayWinGateNum=0;
                UserInfo.taskIsTodayReceiveGateWinOneReward=false;
            }
        }
        else {
            let str = this.nameAllArr[randomNum(0, this.nameAllArr.length - 1)];
            let avatarIndex = randomNum(0, this.avatarTotalNum - 1);
            UserInfo.userName = str;
            UserInfo.userAvatar = avatarIndex;

            this.saveGameData();

             //记录一下登录时间
             const now = new Date();
             const date = new Date(now.getFullYear(), now.getMonth(), now.getDate());
             localStorage.setItem('loginTodayTime', date.toLocaleString());
 
        }

        this.initHomeUI();

    }

    onclickstartMathGameBtn() {
        if (UserInfo.staminaNum<=0) {
            this.createMinTip("体力不足");
        }
        else{
            UserInfo.staminaNum-=1;
            this.saveGameData();
            this.initHomeUI();
            UILayerMgr.getIns.openUILayer("matchPanel");
        }
    }

    onclickstartGateGameBtn() {
        if (UserInfo.staminaNum<=0) {
            this.createMinTip("体力不足");
        }
        else{
            UserInfo.staminaNum-=1;
            this.saveGameData();
            this.initHomeUI();
            UILayerMgr.getIns.openUILayer("gateGamePanel");
        }
    }

    saveGameData(){
        let data={
            userName:UserInfo.userName,
            userAvatar:UserInfo.userAvatar,
            staminaNum:UserInfo.staminaNum,
            taskIsTodayReceiveReward:UserInfo.taskIsTodayReceiveReward,
            todayWinPkGameNum:UserInfo.todayWinPkGameNum,
            taskIsTodayReceiveWinOnePkReward:UserInfo.taskIsTodayReceiveWinOnePkReward,
            taskTodayWinGateNum:UserInfo.taskTodayWinGateNum,
            gateLevel:UserInfo.gateLevel,
            taskIsTodayReceiveGateWinOneReward:UserInfo.taskIsTodayReceiveGateWinOneReward,
        }
        localStorage.setItem("userInfo",JSON.stringify(data));
    }

    createMinTip(str){
        let pre=instantiate(this.minTipPre);
        pre.setParent(find("Canvas/minTipRoot"));
        pre.getChildByName("Label").getComponent(Label).string=str;

        tween(pre)
        .by(0.6,{position:v3(0,50,0)})
        .call(()=>{
            pre.destroy();
        })
        .start();
    }

    onclickTaskBtn(){
        UILayerMgr.getIns.openUILayer("taskPanel");
    }
}


